package com.cunjin.student.comtroller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cunjin.base.controller.BaseController;
import com.cunjin.base.utils.DuanxinUtil;
import com.cunjin.student.entity.StudentEntity;
import com.cunjin.student.service.StudentService;


@Controller
@RequestMapping("/student")
public class StudentComtroller extends BaseController<StudentEntity> {

	private String yzm="";
	
	@Autowired
	private StudentService service;
	
		//http://localhost:8090/cunjin-dorm/student/myqueryAll
		@RequestMapping("/myqueryAll")
		@ResponseBody
		public Map<String, Object> myqueryAll(StudentEntity entity) {
			
			Map<String, Object> all = service.queryAll();
			return all;
		}
		
		//登陆功能
//http://localhost:8090/cunjin-dorm/student/mylogin
		@RequestMapping("/mylogin")
		@ResponseBody
		public Map<String, Object>  mylogin(StudentEntity entity,HttpServletRequest req) {
			
			Map<String, Object>  myinsert = service.login(entity,req);
			return myinsert;
		}
		
//http://localhost:8090/cunjin-dorm/student/myreg
		@RequestMapping("/myreg")
		@ResponseBody
		public Map<String, Object> myreg(StudentEntity entity) {
			Map<String, Object>  myinsert = service.queryByloginNo(entity);
			return myinsert;
		}
		

		@RequestMapping("/insertStudent")
		@ResponseBody
		public Map<String, Object> insertStudent(StudentEntity entity) {
			Map<String, Object>  myinsert = service.insertStudent(entity);
			return myinsert;
		}
		
		/**
		 * 分页
		 * @param page
		 * @param rows
		 * @param searchName
		 * @return
		 */
		@RequestMapping("/findWithPage")
		@ResponseBody
		public Map<String, Object>  findWithPage(int page,int rows,String searchName) {
			
			Map<String, Object> pages=new HashMap<String, Object>();
			
			int before=(page-1)*rows;
			int after=rows;
			pages.put("before", before);
			pages.put("after", after);
			if(null !=searchName){
				pages.put("searchName", searchName+"%");
			}else{
				pages.put("searchName", null);
			}
			
			Map<String, Object>  fy = service.queryByPage(pages);
			return fy;
		}
		
		
		/**
		 * 添加一个学生（多表）
		 * @param page
		 * @param rows
		 * @return
		 */
		@RequestMapping("/addStudent")
		@ResponseBody
		public Map<String, Object>  addStudent(StudentEntity entity) {
			Map<String, Object>  myinsert = service.addStudent(entity);
			return myinsert;
		}
		
		/**
		 * 
		 * 删除
		 * @param dles
		 * @return
		 */
		@RequestMapping("/delStudent")
		@ResponseBody
		public Map<String, Object>  delStudent(String dles) {
			
			String[] split = dles.split(",");
			Map<String, Object>  mydelete = service.deleteByids(split);
			return mydelete;
		}
		
		/**
		 * 更新一个学生
		 * @param entity
		 * @return
		 */
		@RequestMapping("/updateStudent")
		@ResponseBody
		public Map<String, Object>  updateStudent(StudentEntity entity) {
			
			Map<String, Object>  myupdate = service.updateStudent(entity);
			return myupdate;
		}
		
		/**
		 * 获得手机验证码
		 * @param entity
		 * @return
		 */
		@RequestMapping("/phoneyanzheng")
		@ResponseBody
		public Map<String, Object>  phoneyanzheng(String phonenum,HttpServletRequest req) {
			
			Map<String, Object> map=new HashMap<String, Object>();
			try {
				int code = DuanxinUtil.getCode();
				DuanxinUtil.phoneSecurity(code, phonenum);
				HttpSession session = req.getSession();
				session.setAttribute("yzm", code);
				map.put("yzm", code);
				map.put("code", 200);
				yzm=""+code;
				
			} catch (IOException e) {
				e.printStackTrace();
			}
			return map;
			
		}
		
		/**
		 * 验证手机验证码
		 * @param entity
		 * @return
		 */
		@RequestMapping("/checkYanZheng")
		@ResponseBody
		public Map<String, Object>  checkYanZheng(String qyzm,HttpServletRequest req) {
			
			Map<String, Object> map=new HashMap<String, Object>();
			try {
				HttpSession session = req.getSession();
				String syzm =(String)session.getAttribute("yzm");
				if(qyzm.equals(yzm)){
					map.put("code", 200);
					map.put("flag", 1);
				}else{
					map.put("code", 500);
					map.put("flag", 0);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return map;
			
		}
		
		
}
